//app.js
var http = require('utils/http.js')   
var ispoint=0;
var app=getApp();
var that;
App({
 //local_url:"https://mtt.fengme.cc/maott/app/index.php?i=1&c=entry&m=ewei_shop&", 
 local_url:"https://maotuotuo.com/maott/app/index.php?i=1&c=entry&m=ewei_shop&", 
 
 // local_url: "http://maott/maott/app/index.php?i=1&c=entry&m=ewei_shop&",   

  data: {   //全局变量
    appId:'wx34107fc1b56e9735',
    secret:'9fcadaa4a5d6403f92cfdde698d7c12c',
    ser_selected:[],  //酒店服务被选中的项目
    person_msg_rr:'',
    openid:"",
    signature:"",
    iswarm:0,  //是否弹出过提醒注册会员的界面，0没有，1已提醒一次,2就不用管了
    vs:'',  //该程序版本号，版本不同可刷新
    over_vs:false, //版本是否过时
    to_ser:0, //判断是否从看猫界面直接跳到添加服务界面

    formid: '', //用于发送模板消息的
    pointed: true, //判断能否输入密码，密码6为即设为false，1s后再设为真
    ordersn:'', //住房时猫数量较少，留下订单提醒加猫，加完猫咪后跳订单详情
   
  },
  onLaunch: function () {
    
   
       that=this
       this.getUserInfo(function (res) {
          // console.log(res);
          // console.log(that.globalData.userInfo);
          that.data.openid=wx.getStorageSync('openid');
        });
        var session_id = wx.getStorageSync('PHPSESSID');//本地取存储的sessionID
        // console.log(session_id);

        if (session_id != "" && session_id != null) {
          wx.removeStorageSync('PHPSESSID')
        }

        
        // 获取shop_id  
        
   
     
  },
  getUserInfo: function (cb, cp) {
    if (this.globalData.userInfo && wx.getStorageSync('outtime') > parseInt(Date.now() / 1000)) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.checkSession({
        success: function () {
          //session 未过期，并且在本生命周期一直有效
          wx.login({
            success: function (res) {
              if (res.code) {
                //发起网络请求
                console.info('进来了::');
                http.check_member(res.code, that, cb, cp)
              } else {
                console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        },
        fail: function () {
          //登录态过期
          //重新登录
          wx.login({
            success: function (res) {
              if (res.code) {
                http.check_member(res.code, that, cb, cp)
              } else {
                http.toast('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        }
      })
    }
  },
 
  globalData: {  //个人全局信息
    userInfo: null
  },
  hotelData: {  //酒店全局信息,在home界面赋值
    house_type:null,    //房间分类
    house_notict: null   //入住须知
  },
  clear_overtime_order: function (){  //删除所有过时订单
    // console.log(that.data.openid);
    that.req("del_overtime_order.html", {'openid': that.data.openid},
      function (res) {  //删除未付款订单
        // console.log(res);
        if (res.status == "error") {
          // console.log(res.mess);
          return;
        }
      }, function (res) {
        that.clear_overtime_order()//失败重新调用
      });
  },
  update_person_msg: function (k,v) {
    console.log(v);
    this.req("update_person_msg.html", {openid:this.globalData.userInfo.openid,feild:k,value:v}, function (res) {
      // console.log(res);
    }, function (res) {
      that.update_person_msg(k,v)//失败重新调用
    });
  },
  update_cat_msg: function (k, v, c) {
    // console.log(v);
    that.req("update_cat_msg.html", { openid: that.globalData.userInfo.openid, feild: k, value: v, cat_id:c}, function (res) {
      // console.log(res);
    }, function (res) {
      console.log(res);
    });
  },
  get_msg_rr:function(cb){
    var openid = wx.getStorageSync('openid');
    that.globalData.userInfo=null;
    that.getUserInfo(function (res) {
      
      that.req("get_person_f_rr.html", { "openid": openid}, function (res) {
        if (res.status == "success") {
          that.data.person_msg_rr=res.mess
          typeof cb == "function" && cb()
        } else if (res.status == "error") {
          that.toast(res.mess)
        } else {
          that.modal("错误提示", "网络错误，是否重试？", function (re) {
            if (re) {
              that.get_msg_rr()
            }
          })
        }
      })
    }, function () {
      that.get_msg_rr()
    });
  },
  get_p_msg: function (oid) {  //获取个人所有信息
  var that=this;
    that.req("get_p.html", { "openid": oid }, function (res) {
      if (res.status == "success") {
        //console.info("userinfo: "+res.mess);
        that.globalData.userInfo = res.mess;  
      } else if (res.status == "error") {
        that.toast_up(res.mess)
      } else {
        that.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_hotel_msg()
          }
        })
      }
    })
  },
  check_vs: function () {  //检查版本
    var that = this;
    that.req("get_vs.html", {  }, function (res) {
      // console.log(res)
      if (res.status == "success") {
        if(that.data.vs==''){
          that.data.vs = res.vs //版本赋值
          that.data.over_vs = false; //版本是否过时
          
        } else if (that.data.vs != res.vs){//版本不一，提醒跟新
          that.modal("提示", "猫托托出新版本了！为了您更好的体验，请重新搜索进入猫托托。")
          that.data.over_vs = true;
        }
      } else if (res.status == "error") {
        that.toast_up(res.mess)
      } else {
        that.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.check_vs();
          }
        })
      }
    })
  },
  orderSign: function (price, type_remark,ordersn) {  //向用户微信上发送模板消息
    that.req("send_Token.html", { "openid": that.data.openid, "formid": that.data.formid, "price": price, "type_remark": type_remark, "ordersn": ordersn }, function (res) {
      console.log(222)
      console.log(res)
      if (res.status == "success") {

      } else if (res.status == "error") {

      } else {
        that.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.orderSign()
          }
        })
      }
    })
  },
  toast_up: function (s) {  //带感叹号的提示框
      wx.showToast({
        title: s,
        image: '../../images/g.png',
        duration: 1500
      })
  },
  outtime: 0,
  req: http.req,
  modal: http.modal,
  toast: http.toast,
  //rootDocment: 'https://mtt.fengme.cc/tp/brn_mtt/mttadmin/ajax/'
  rootDocment: 'https://maotuotuo.com/tp/brn_mtt/mttadmin/ajax/'
 // rootDocment: 'http://maott/tp/brn_mtt/mttadmin/ajax/'
})